<?php
/**
 *  Create On 2015-6-11
 *  Author jing
 **/


class UploadService extends BaseService
{
    private static $self = NULL;

    static public function instance()
    {
        if (self::$self == NULL) {
            self::$self = new self();
        }

        return self::$self;
    }

    public function upload_audio($file)
    {
    	#文件上传
    	$clientName = $file->getClientOriginalName();
    	$extension  = $file->getClientOriginalExtension()?$file->getClientOriginalExtension():"aac";
    	$newName    = md5(date('ymdhis').$clientName).".".$extension;
    	$file->move(public_path().'/resource/attach',$newName);
    	
    	if($file->getError()){
    		throw new Exception('附件上传失败！', ErrorCodeEnum::STATUS_ERROR);
    	}
    	//上传七牛
    	$qiniuConf = Config::get("qiniu");
    	QiniuUtil::instance()->setConfig($qiniuConf['accessKey'], $qiniuConf['secretKey'],$qiniuConf['bucket_audio']);
    	$result = QiniuUtil::instance()->upload_audio($newName, public_path().'/resource/attach/'.$newName);
    	if ($result){
    		#记录文件
    		$aData = array(
	    		'name' => $newName,
	    		'time' => date('y-m-d H:i:s', time())
    		);
    		return $this->mAudioInfo->insert($aData);    		
    	}else throw new Exception('附件上传失败！', ErrorCodeEnum::STATUS_ERROR);
    }
    	
}